package com.zzw.utils.opfile;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.zzw.model.StudentDO;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * @author zzw
 * @version 1.0
 * @title FileStreamOperation
 * @description <description class purpose>
 * @datetime 2022-12-28 8:50 下午
 */
public class FileStreamOperation {

    private FileStreamOperation() {
    }

    /**
     * 存储学生信息到文件中
     *
     * @param studentDO 学生信息对象
     * @param tagFile   目标文件
     * @return 是否写入成功  true-是, false-否
     */
    public static boolean writeInfoToFile(StudentDO studentDO, String tagFile) {
        try (ObjectOutputStream objOutStream = new ObjectOutputStream(new FileOutputStream(tagFile))) {
            objOutStream.writeObject(studentDO);
        } catch (IOException e) {
            log("writeInfoToFile", "写入", e.getMessage());
            return false;
        }

        return true;
    }

    /**
     * 存储学生信息列表到文件中
     *
     * @param studentDOList 学生信息列表
     * @param tagFile       目标文件
     * @return 是否写入成功  true-是, false-否
     */
    public static boolean writeInfosToFile(List<StudentDO> studentDOList, String tagFile) {
        try (ObjectOutputStream objOutStream = new ObjectOutputStream(new FileOutputStream(tagFile))) {
            objOutStream.writeObject(studentDOList);
            objOutStream.flush();
        } catch (IOException e) {
            log("writeInfosToFile", "写入", e.getMessage());
            return false;
        }

        return true;
    }

    /**
     * 查询所有学生信息
     *
     * @param tagFile 目标文件
     * @return 学生信息列表
     */
    public static List<StudentDO> readInfosFromFile(String tagFile) {
        File file = new File(tagFile);
        // 空文件判断，防止 EOF 异常
        if (file.exists() && file.length() == 0) {
            return new ArrayList<>();
        }

        try (ObjectInputStream objOutStream = new ObjectInputStream(new FileInputStream(file))) {
            return JSON.parseObject(JSON.toJSONString(objOutStream.readObject()),
                    new TypeReference<List<StudentDO>>() {
                    });
        } catch (IOException | ClassNotFoundException e) {
            log("readInfosFromFile", "读取", e.getMessage());
            return new ArrayList<>();
        }
    }

    /**
     * 查询学生信息
     *
     * @param tagFile 目标文件
     * @return 学生信息对象
     */
    public static StudentDO readInfoFromFile(String tagFile) {
        File file = new File(tagFile);
        if (file.exists() && file.length() == 0) {
            return new StudentDO();
        }

        try (ObjectInputStream objOutStream = new ObjectInputStream(new FileInputStream(tagFile))) {
            return (StudentDO) objOutStream.readObject();
        } catch (IOException | ClassNotFoundException e) {
            log("readInfoFromFile", "读取", e.getMessage());
            return new StudentDO();
        }
    }


    private static void log(String method, String type, String errorMsg){
        String messageFormat = "【FileStreamOperation#%s】%s文件异常, 错误信息 e=%s";
        String message = String.format(messageFormat, method, type, errorMsg);
        System.err.println(message);
    }
}
